

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs');


app.listen(8080);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  i=0;
  console.log(io.sockets.clients().server.eio.clientsCount);
  // console.log(io.sockets.rooms());
  //console.log("connection")
  socket.emit('welcome', { welcome: 'welcome!!!!!!' });
  //console.log("send welcome")
  // socket.on('my other event', function (data) {
  //   console.log(data);
  // });

  // socket.set('nickname', 'nickname', function(){
  // });

  // socket.on('set nickname', function(name){
  //   socket.set('nickname', name, function(){
  //     console.log("change nickname=>" + name);
  //     socket.emit('nickname ready');
  //   });
  // });

  socket.on('send', function(data){
    console.log("re send data.message")
    // socket.get('nickname', function(err, name){

    // socket.emit("notice", {message: name + ":" + data.message});
    // socket.broadcast.emit("notice", {message: name + ":" + data.message});
    // console.log("chat: " + data.message);
    // });

    console.log("broadcast start")
    io.sockets.emit("msg", {message: data.message});
    console.log("broadcast end")

  });

  socket.on("disconnect",function(){
    console.log("disconnect")
  });



});